package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.data.Page;
import com.dongdong.data.Result;
import com.dongdong.em.ResultEmnu;
import com.dongdong.pojo.TbContent;
import com.dongdong.pojo.TbContentCategory;
import com.dongdong.service.ConCategoryService;
import com.dongdong.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("contentController")
public class ContentController {
    @Reference(timeout = 5000)
    private ContentService contentService;
    @Reference(timeout = 5000)
    private ConCategoryService conCategoryService;

    @RequestMapping("toContent")
    public String toContent(Model model){
        List<TbContentCategory> list =  conCategoryService.selectAll();
        model.addAttribute("list",list);
        return "admin/content";
    }
    @RequestMapping("contentList")
    @ResponseBody
    public Result contentList(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "4")Integer pageSize){
        Page<TbContent> contentList = contentService.selectInfo(pageNum,pageSize);
        return Result.respone(ResultEmnu.SUCCESS).setData(contentList);
    }
    @RequestMapping("increase")
    @ResponseBody
    public Result increase(TbContent tbContent){
        try{
            int i = contentService.insert(tbContent);
            return Result.respone(ResultEmnu.SUCCESS);
        }catch (Exception e){
            return Result.respone(ResultEmnu.ERROR);
        }
    }
    @RequestMapping("toUpdate/{id}")
    @ResponseBody
    public Result toUpdate(@PathVariable("id")Long id){
        TbContent tbContent = contentService.selectById(id);
        return Result.respone(ResultEmnu.SUCCESS).setData(tbContent);
    }
    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(TbContent tbContent){
        int i = contentService.updateInfo(tbContent);
        if (i > 1){
            return Result.respone(ResultEmnu.SUCCESS);
        }else {
            return Result.respone(ResultEmnu.ERROR);
        }
    }
    @RequestMapping("deleteBatch/{ids}")
    @ResponseBody
    public Result deleteBatch(@PathVariable("ids")Long[] ids){
        int i = contentService.deleteBatch(ids);
        System.out.println(Result.respone(ResultEmnu.SUCCESS));
        if (i > 1){
            return Result.respone(ResultEmnu.SUCCESS);
        }else {
            return Result.respone(ResultEmnu.ERROR);
        }
    }

}
